package org.example.node;

/**
 * @author liyishan
 * @date 2024/9/27 10:42
 * @apiNote
 */

public class Test00021MergeTwoLists {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy = new ListNode(-1);
        ListNode prev = dummy;
        while (list1 != null && list2 != null){
            if(list1.val <= list2.val) {
                prev.next = list1;
                list1 = list1.next;
            }else{
                prev.next = list2;
                list2 = list2.next;
            }
            prev = prev.next;
        }
        prev.next = list1 == null ? list2 : list1;
        return dummy.next;
    }
}
